<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>iOSUIKit之UIWindow详解 | Albert Yuan's Blog</title><meta name="keywords" content="swift"><meta name="author" content="Albert Yuan"><meta name="copyright" content="Albert Yuan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="referrer" content="no-referrer"><meta name="description" content="1.UIWindow简介  UIWindow是一种特殊的UIView,通常在一个app中至少会有一个UIWindow。 iOS程序启动完毕后，创建的第一个视图控件就是UIWindow，接着创建控制器的View,最后将控制器的View添加到UIWindow上，于是控制器的View就显示在屏幕上了。 一个iOS程序之所以能显示在屏幕上，完全是因为它有UIWindow，也就是说，没有UIWindow就看">
<meta property="og:type" content="article">
<meta property="og:title" content="iOSUIKit之UIWindow详解">
<meta property="og:url" content="https://yllove.gitee.io/2021/09/24/iOS%20UIKIt/iOSUIKit%E4%B9%8BUIWindow%E8%AF%A6%E8%A7%A3/index.html">
<meta property="og:site_name" content="Albert Yuan&#39;s Blog">
<meta property="og:description" content="1.UIWindow简介  UIWindow是一种特殊的UIView,通常在一个app中至少会有一个UIWindow。 iOS程序启动完毕后，创建的第一个视图控件就是UIWindow，接着创建控制器的View,最后将控制器的View添加到UIWindow上，于是控制器的View就显示在屏幕上了。 一个iOS程序之所以能显示在屏幕上，完全是因为它有UIWindow，也就是说，没有UIWindow就看">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages2018.cnblogs.com%2Fblog%2F749924%2F201807%2F749924-20180730112644434-125751676.png&refer=http%3A%2F%2Fimages2018.cnblogs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635046008&t=fd235aaa9fc1476b26197b2443c90864">
<meta property="article:published_time" content="2021-09-24T03:24:12.714Z">
<meta property="article:modified_time" content="2021-09-24T06:10:48.649Z">
<meta property="article:author" content="Albert Yuan">
<meta property="article:tag" content="iOS">
<meta property="article:tag" content="UIKit">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages2018.cnblogs.com%2Fblog%2F749924%2F201807%2F749924-20180730112644434-125751676.png&refer=http%3A%2F%2Fimages2018.cnblogs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635046008&t=fd235aaa9fc1476b26197b2443c90864"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://yllove.gitee.io/2021/09/24/iOS%20UIKIt/iOSUIKit%E4%B9%8BUIWindow%E8%AF%A6%E8%A7%A3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/instantsearch.js@2.10.5/dist/instantsearch.min.css" media="print" onload="this.media='all'"><script src="https://fastly.jsdelivr.net/npm/instantsearch.js@2.10.5/dist/instantsearch.min.js" defer></script><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"X8NS0XSX99","apiKey":"b9d4530647c8b5bf7928a04c47752ad7","indexName":"albertyuan","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://fastly.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://fastly.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://fastly.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://fastly.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'iOSUIKit之UIWindow详解',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2021-09-24 14:10:48'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/blog/atom.xml" title="Albert Yuan's Blog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/blog/img/favicon.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">155</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">56</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">32</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages2018.cnblogs.com%2Fblog%2F749924%2F201807%2F749924-20180730112644434-125751676.png&amp;refer=http%3A%2F%2Fimages2018.cnblogs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1635046008&amp;t=fd235aaa9fc1476b26197b2443c90864')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">Albert Yuan's Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">iOSUIKit之UIWindow详解</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-24T03:24:12.714Z" title="发表于 2021-09-24 11:24:12">2021-09-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-09-24T06:10:48.649Z" title="更新于 2021-09-24 14:10:48">2021-09-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/UIKit/">UIKit</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="iOSUIKit之UIWindow详解"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-UIWindow简介"><a href="#1-UIWindow简介" class="headerlink" title="1.UIWindow简介"></a>1.UIWindow简介</h2><blockquote>
<ol>
<li>UIWindow是一种特殊的UIView,通常在一个app中至少会有一个UIWindow。</li>
<li>iOS程序启动完毕后，创建的第一个视图控件就是UIWindow，接着创建控制器的View,最后将控制器的View添加到UIWindow上，于是控制器的View就显示在屏幕上了。</li>
<li>一个iOS程序之所以能显示在屏幕上，完全是因为它有UIWindow，也就是说，没有UIWindow就看不到任何UI界面。</li>
<li>状态栏和键盘都是特殊的UIWindow。</li>
</ol>
</blockquote>
<p>那么UIWindow是如何将View显示到屏幕上的呢</p>
<blockquote>
<p>这里有三个重要的对象UIScreen，UIWindow，UIView：<br> UIScreen对象识别物理屏幕连接到设备<br> UIWindow对象提供绘画支持给屏幕<br> UIView执行绘画，当窗口要显示内容的时候，UIView绘画出他们的内容并附加到窗口上。这样View就显示在窗口上了。</p>
</blockquote>
<p>1、<strong>添加 UIView 到 UIWindow 中两种常见方式：</strong></p>
<p>（1）- (void)addSubview:(UIView )view;*</p>
<p>直接将 view 添加到 UIWindow 中，但并不会理会 view 对应的 UIViewController；</p>
<p>（2）@property(nonatomic,retain) UIViewController *rootViewController;</p>
<p>自动将 rootViewController 的 view 添加到 UIWindow 中，负责管理 rootViewController 的生命周期。</p>
<p>2、<strong>UIWindow 常用方法</strong></p>
<p><em>- (void)makeKeyWindow;</em></p>
<p>让当前 UIWindow 变成 keyWindow（主窗口）；</p>
<p><em>- (void)makeKeyAndVisible;</em></p>
<p>让当前 UIWindow 变成 keyWindow，并显示出来。</p>
<p>3、<strong>UIWindow 的获得</strong></p>
<p>[UIApplication sharedApplication].windows</p>
<p>在本应用中打开的 UIWindow 列表，这样就可以接触应用中的任何一个 UIView 对象；(平时输入文字弹出的键盘，就处在一个新的 UIWindow 中)</p>
<p>[UIApplication sharedApplication].keyWindow</p>
<p>用来接收键盘以及非触摸类的消息事件的 UIWindow，而且程序中每个时刻只能有一个 UIWindow 是 keyWindow。如果某个 UIWindow 内部的文本框不能输入文字，可能是因为这个 UIWindow 不是 keyWindow。</p>
<hr>
<h2 id="2-UIWindow的创建"><a href="#2-UIWindow的创建" class="headerlink" title="2.UIWindow的创建"></a>2.UIWindow的创建</h2><h4 id="1-UIWindow是什么时候创建的"><a href="#1-UIWindow是什么时候创建的" class="headerlink" title="1.UIWindow是什么时候创建的?"></a>1.UIWindow是什么时候创建的?</h4><p>我们可以发现，当我们新建一个项目，直接在stroyboard为view设置一个背景颜色，然后运行项目，就能看到换了背景颜色的view，这说明系统已经帮我们创建了一个UIWindow，那么这个UIWindow是什么时候创建的？</p>
<p>我们找到程序的入口<code>main</code>函数，来看程序的启动过程</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> main(<span class="type">int</span> argc, <span class="type">char</span> * argv[]) &#123;</span><br><span class="line">    <span class="keyword">@autoreleasepool</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">UIApplicationMain</span>(argc, argv, <span class="literal">nil</span>, <span class="built_in">NSStringFromClass</span>([AppDelegate <span class="keyword">class</span>]));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>此时我们可以根据UIApplicationMain函数了解程序启动的过程</p>
<blockquote>
<ol>
<li>根据传递的类名创建UIApplication对象，这是第一个对象</li>
<li>创建UIApplication代理对象，并给UIApplicaiton对象设置代理</li>
<li>开启主运行循环 main events loop处理事件，保持程序一直运行</li>
<li>加载info.plist,判断是否指定mian(xib 或者 storyboard)如果指定就去加载</li>
</ol>
</blockquote>
<p>当我们把指定的Main Interface 中mian给删除的时候，重新运行程序，就会发现我们之前设置的view没有办法显示了。</p>
<p><img src= "" data-lazy-src="https://upload-images.jianshu.io/upload_images/1434508-20002ffcc5af33b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/880/format/webp" alt="img"></p>
<p>Main Interface 中 Main删除</p>
<p>此时我们基本可以想到，UIWindow应该是在加载storyboard的时候系统创建的，那么系统是如何加载storyboard的呢？<br> 系统在加载storyboard的时候会做以下三件事情</p>
<blockquote>
<ol>
<li>创建窗口</li>
<li>加载mian.storyboard 并实例化view controller</li>
<li>分配新视图控制器到窗口root viewcontroller,然后使窗口显在示屏幕上。</li>
</ol>
</blockquote>
<p>因此，当系统加载完info.plist，判断后发现没有main，就不会加载storyboard，也就不会帮我们创建UIWindow，那么我们需要自己在程序启动完成的时候也就是在<code>didFinishLaunchingWithOptions</code>方法中创建。</p>
<h4 id="2-如何创建UIWindow"><a href="#2-如何创建UIWindow" class="headerlink" title="2.如何创建UIWindow?"></a>2.如何创建UIWindow?</h4><p>首先根据系统加载storyboard时做的三件事情，我们可以总结出UIWindow创建步骤</p>
<blockquote>
<ol>
<li>创建窗口对象</li>
<li>创建窗口的根控制器，并且赋值</li>
<li>显示窗口</li>
</ol>
</blockquote>
<p>并且我们在<code>AppDelegate.h</code>中发现属性window</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@property</span> (<span class="keyword">strong</span>, <span class="keyword">nonatomic</span>) <span class="built_in">UIWindow</span> *window;</span><br></pre></td></tr></table></figure>

<p>那么我们来看一下如何创建UIWindow</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="type">BOOL</span>)application:(<span class="built_in">UIApplication</span> *)application didFinishLaunchingWithOptions:(<span class="built_in">NSDictionary</span> *)launchOptions &#123;</span><br><span class="line">    <span class="comment">//创建窗口对象</span></span><br><span class="line">    <span class="keyword">self</span>.window = [[<span class="built_in">UIWindow</span> alloc]initWithFrame:[<span class="built_in">UIScreen</span> mainScreen].bounds];</span><br><span class="line">    <span class="comment">//创建窗口的根控制器，并且赋值</span></span><br><span class="line">    <span class="built_in">UIViewController</span> *rootVc = [[<span class="built_in">UIViewController</span> alloc]init];</span><br><span class="line">    <span class="keyword">self</span>.window.rootViewController = rootVc;</span><br><span class="line">    <span class="comment">//显示窗口</span></span><br><span class="line">    [<span class="keyword">self</span>.window makeKeyAndVisible];</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">YES</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>窗口显示注意点：</p>
<ol>
<li>我们看到系统为我们创建的window属性是strong强引用，是为了不让窗口销毁，所以需要强引用</li>
<li>窗口的尺寸必须设置，一般设置为屏幕大小。</li>
<li><code>[self.window addsubview:rootVc.view];</code>可直接将控制器的view添加到UIWindow中，并不理会它对应的控制器，但是这种方法违背了MVC原则，当我们需要处理一些业务逻辑的时候就很麻烦了。</li>
<li>当发生屏幕旋转事件的时候，UIapplication对象会将旋转事件传递给UIWindow，UIWindow又会将旋转事件传递给它的根控制器，由根控制器决定是否需要旋转。UIapplication对象 -&gt; UIWindow -&gt; 根控制器。<br> （<code>[self.window addsubview:rootVc.view];</code>没有设置根控制器，所以不能跟着旋转）。</li>
<li>设置根控制器可以将对应界面的事情交给对应的控制器去管理。</li>
</ol>
</blockquote>
<p>那么**[self.window makeKeyAndVisible];**这个方法为什么就能显示窗口呢？我们来看一下<code>[self.window makeKeyAndVisible];</code>的底层实现了哪些功能</p>
<blockquote>
<ol>
<li>可以显示窗口</li>
<li>成为应用程序的主窗口</li>
</ol>
</blockquote>
<p>当我们不调用这个方法，打印self.window。</p>
<blockquote>
<p>UIWindow: 0x7f920503cc80; frame &#x3D; (0 0; 414 736); hidden &#x3D; YES; gestureRecognizers &#x3D; &lt;NSArray: 0x7f92050332a0&gt;; layer &#x3D; &lt;UIWindowLayer: 0x7f920503ad50&gt;&gt;</p>
</blockquote>
<p>我们可以看到 <code>hidden = YES;</code>那么<code>hidden = NO</code>就可以显示窗口了<br> 另外，我们在<code>[self.window makeKeyAndVisible];</code>前后分别输出一下<code>application.keyWindow</code></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">NSLog(@&quot;%@&quot;,application.keyWindow);</span><br><span class="line"><span class="selector-attr">[self.window makeKeyAndVisible]</span>;</span><br><span class="line">NSLog(@&quot;%@&quot;,application.keyWindow);</span><br></pre></td></tr></table></figure>

<p>打印内容</p>
<blockquote>
<p>UIWindow[6259:1268399] (null)<br> UIWindow[6259:1268399] &lt;UIWindow: 0x7fefdb529b30; frame &#x3D; (0 0; 414 736); gestureRecognizers &#x3D; &lt;NSArray: 0x7fefdb529e40&gt;; layer &#x3D; &lt;UIWindowLayer: 0x7fefdb529ae0&gt;&gt;</p>
</blockquote>
<p>我们可以看到调用<code>[self.window makeKeyAndVisible];</code>方法之后<code>application.keyWindow</code>就有值了，那么<code>[self.window makeKeyAndVisible];</code>的底层实现就很明显了。</p>
<blockquote>
<ol>
<li>可以显示窗口  <code>self.window.hidden = NO;</code></li>
<li>成为应用程序的主窗口 <code>application.keyWindow = self.window</code>，这个会报错，因为<code>application.keyWindow</code>是readonly，所以我们没有办法直接赋值。</li>
</ol>
</blockquote>
<h4 id="3-通过storyboard、xib加载控制器"><a href="#3-通过storyboard、xib加载控制器" class="headerlink" title="3.通过storyboard、xib加载控制器"></a>3.通过storyboard、xib加载控制器</h4><p>刚才我们提到过系统在加载storyboard的时候会做以下三件事情</p>
<blockquote>
<ol>
<li>创建窗口</li>
<li>加载mian.storyboard或者xib， 并实例化view controller</li>
<li>分配新视图控制器到窗口root viewcontroller,然后使窗口显在示屏幕上。</li>
</ol>
</blockquote>
<p>那么我们用代码来模拟实现一下</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="type">BOOL</span>)application:(<span class="built_in">UIApplication</span> *)application didFinishLaunchingWithOptions:(<span class="built_in">NSDictionary</span> *)launchOptions &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1.创建窗口</span></span><br><span class="line">    <span class="keyword">self</span>.window = [[<span class="built_in">UIWindow</span> alloc] initWithFrame:[<span class="built_in">UIScreen</span> mainScreen].bounds];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 2.方式一：加载main.storyboard,创建main.storyboard描述的控制器</span></span><br><span class="line">    <span class="comment">// UIStoryboard专门用来加载stroyboard</span></span><br><span class="line">    <span class="comment">// name:storyboard名称不需要后缀</span></span><br><span class="line">    <span class="built_in">UIStoryboard</span> *stroyboard = [<span class="built_in">UIStoryboard</span> storyboardWithName:<span class="string">@&quot;Main&quot;</span> bundle:<span class="literal">nil</span>];</span><br><span class="line">    <span class="comment">// 加载sotryboard描述的控制器</span></span><br><span class="line">    <span class="comment">// 加载箭头指向的控制器</span></span><br><span class="line">    <span class="built_in">UIViewController</span> *vc = [stroyboard instantiateInitialViewController];</span><br><span class="line">    <span class="comment">//根据绑定标识加载 </span></span><br><span class="line">    <span class="comment">//UIViewController *vc = [stroyboard instantiateViewControllerWithIdentifier:@&quot;red&quot;];</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">//2.方式二 通过xib创建控制器</span></span><br><span class="line">    <span class="comment">//ViewController *vc = [[ViewController alloc] initWithNibName:@&quot;VC&quot; bundle:nil];</span></span><br><span class="line">  </span><br><span class="line">    <span class="comment">// 设置窗口的根控制器</span></span><br><span class="line">    <span class="keyword">self</span>.window.rootViewController = vc;</span><br><span class="line">    <span class="comment">// 3.显示窗口</span></span><br><span class="line">    [<span class="keyword">self</span>.window makeKeyAndVisible];</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="literal">YES</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="3-UIWindow的层级"><a href="#3-UIWindow的层级" class="headerlink" title="3.UIWindow的层级"></a>3.UIWindow的层级</h2><p>UIWindow是有层级的，层级高的显示在最外面，当层级相同时，越靠后调用的显示在外面。</p>
<p>UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal; &#x2F;&#x2F;默认，值为0<br> UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert; &#x2F;&#x2F;值为2000<br> UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar ; &#x2F;&#x2F; 值为1000</p>
<p>所以<code>UIWindowLevelNormal &lt; UIWindowLevelStatusBar&lt; UIWindowLevelAlert </code><br>并且层级是可以做加减的<code>self.window.windowLevel = UIWindowLevelAlert+1;</code></p>
<hr>
<h3 id="4-UIWindow的实际应用"><a href="#4-UIWindow的实际应用" class="headerlink" title="4.UIWindow的实际应用"></a>4.UIWindow的实际应用</h3><ol>
<li>APP启动窗口的创建</li>
<li>悬浮电话按钮的应用</li>
</ol>
<hr>
<p>扩展：UIApplication详解</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Albert Yuan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://yllove.gitee.io/2021/09/24/iOS%20UIKIt/iOSUIKit%E4%B9%8BUIWindow%E8%AF%A6%E8%A7%A3/">https://yllove.gitee.io/2021/09/24/iOS%20UIKIt/iOSUIKit%E4%B9%8BUIWindow%E8%AF%A6%E8%A7%A3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://yllove.gitee.io" target="_blank">Albert Yuan's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/iOS/">iOS</a><a class="post-meta__tags" href="/blog/tags/UIKit/">UIKit</a></div><div class="post_share"><div class="social-share" data-image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages2018.cnblogs.com%2Fblog%2F749924%2F201807%2F749924-20180730112644434-125751676.png&amp;refer=http%3A%2F%2Fimages2018.cnblogs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1635046008&amp;t=fd235aaa9fc1476b26197b2443c90864" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://fastly.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/09/24/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/%E5%86%92%E6%B3%A1%E7%AE%97%E6%B3%95%E8%A7%A3%E6%9E%90/"><img class="prev-cover" src= "" data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimbusk.cc%2Fpost%2F6fac06a2%2F26.png&amp;refer=http%3A%2F%2Fnimbusk.cc&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1635043541&amp;t=7d591a5b12f0df28adab00b71935e26b" onerror="onerror=null;src='/blog/img/defaultCover.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">冒泡算法解析</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/09/26/iOS%20UIKIt/UIDynamic%E5%8A%A8%E6%80%81%E7%89%A9%E7%90%86%E5%BC%95%E6%93%8E/"><img class="next-cover" src= "" data-lazy-src="https://img0.baidu.com/it/u=1254289242,26520186&amp;fm=26&amp;fmt=auto" onerror="onerror=null;src='/blog/img/defaultCover.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">UIDynamic动态物理引擎</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2021/09/26/iOS UIKIt/UIDynamic动态物理引擎/" title="UIDynamic动态物理引擎"><img class="cover" src= "" data-lazy-src="https://img0.baidu.com/it/u=1254289242,26520186&fm=26&fmt=auto" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-26</div><div class="title">UIDynamic动态物理引擎</div></div></a></div><div><a href="/blog/2021/10/27/iOS UIKIt/iOS导航栏遮挡问题的总结/" title="iOS导航栏遮挡问题的总结"><img class="cover" src= "" data-lazy-src="/blog/img/defaultCover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-27</div><div class="title">iOS导航栏遮挡问题的总结</div></div></a></div><div><a href="/blog/2021/10/11/iOS UIKIt/iosUIButton自定义图片title位置/" title="iosUIButton自定义图片title位置"><img class="cover" src= "" data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fstatic.17ky.net%2FImages%2F20170529%2F0ba4fd793eeb6bc60d701465ecb80e5b.png&refer=http%3A%2F%2Fstatic.17ky.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636515244&t=7ff70a346e0f2bb2f2c6537f024d404d" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-11</div><div class="title">iosUIButton自定义图片title位置</div></div></a></div><div><a href="/blog/2021/12/21/iOS UIKIt/iosUIImage绘制成圆角图片方式/" title="iosUIImage绘制成圆角图片方式"><img class="cover" src= "" data-lazy-src="/blog/img/defaultCover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-12-21</div><div class="title">iosUIImage绘制成圆角图片方式</div></div></a></div><div><a href="/blog/2021/10/11/iOS UIKIt/ios属性字符串的应用/" title="ios属性字符串的应用"><img class="cover" src= "" data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages.cnblogs.com%2Fcnblogs_com%2Fanee%2F429355%2Fr_jsonToNSDictionary.jpg&refer=http%3A%2F%2Fimages.cnblogs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636511833&t=b52a765fdcefd55c77bda079de2530f5" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-11</div><div class="title">ios属性字符串的应用</div></div></a></div><div><a href="/blog/2021/12/21/iOS UIKIt/ios手势使用问题汇总/" title="ios手势使用问题汇总"><img class="cover" src= "" data-lazy-src="/blog/img/defaultCover.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-12-21</div><div class="title">ios手势使用问题汇总</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/blog/img/favicon.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Albert Yuan</div><div class="author-info__description">博学之,审问之,慎思之,明辨之,笃行之</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">155</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">56</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">32</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/yuanliangYL"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/yuanliangYL" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:yuanliangios@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="qq:1023817403" target="_blank" title="QQ"><i class="fab fa-qq"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">love cat , love life</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-UIWindow%E7%AE%80%E4%BB%8B"><span class="toc-text">1.UIWindow简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-UIWindow%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-text">2.UIWindow的创建</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-UIWindow%E6%98%AF%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E5%88%9B%E5%BB%BA%E7%9A%84"><span class="toc-text">1.UIWindow是什么时候创建的?</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BAUIWindow"><span class="toc-text">2.如何创建UIWindow?</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E9%80%9A%E8%BF%87storyboard%E3%80%81xib%E5%8A%A0%E8%BD%BD%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="toc-text">3.通过storyboard、xib加载控制器</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-UIWindow%E7%9A%84%E5%B1%82%E7%BA%A7"><span class="toc-text">3.UIWindow的层级</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-UIWindow%E7%9A%84%E5%AE%9E%E9%99%85%E5%BA%94%E7%94%A8"><span class="toc-text">4.UIWindow的实际应用</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2023/02/07/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/iOS-%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BD%91%E9%A1%B5%E4%B8%8B%E8%BD%BDIPA%E5%8C%85/" title="iOS 自定义网页下载IPA包"><img src= "" data-lazy-src="https://img.mp.itc.cn/upload/20170409/1a8f793d7282451cbd1beb6841608c90_th.jpeg" onerror="this.onerror=null;this.src='/blog/img/defaultCover.gif'" alt="iOS 自定义网页下载IPA包"/></a><div class="content"><a class="title" href="/blog/2023/02/07/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/iOS-%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BD%91%E9%A1%B5%E4%B8%8B%E8%BD%BDIPA%E5%8C%85/" title="iOS 自定义网页下载IPA包">iOS 自定义网页下载IPA包</a><time datetime="2023-02-07T02:46:29.851Z" title="更新于 2023-02-07 10:46:29">2023-02-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2023/02/06/Flutter/Flutter%E6%8E%A5%E5%85%A5%E5%8E%9F%E7%94%9F-iOS/" title="flutter接入原生-iOS"><img src= "" data-lazy-src="https://pic4.zhimg.com/v2-6a115fea4db1ad770dccf4119bfab36f_1440w.jpg?source=172ae18b" onerror="this.onerror=null;this.src='/blog/img/defaultCover.gif'" alt="flutter接入原生-iOS"/></a><div class="content"><a class="title" href="/blog/2023/02/06/Flutter/Flutter%E6%8E%A5%E5%85%A5%E5%8E%9F%E7%94%9F-iOS/" title="flutter接入原生-iOS">flutter接入原生-iOS</a><time datetime="2023-02-07T01:18:53.661Z" title="更新于 2023-02-07 09:18:53">2023-02-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/12/17/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/iOS%20%E5%A6%82%E4%BD%95%E5%9C%A8%E6%A8%A1%E6%8B%9F%E5%99%A8%E4%B8%AD%E5%AE%89%E8%A3%85APP/" title="iOS 如何在模拟器中安装APP"><img src= "" data-lazy-src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.winwin7.com%2Fuploadfile%2F2021%2F0114%2F20210114141748239.jpg&amp;refer=http%3A%2F%2Fwww.winwin7.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1673854539&amp;t=aad2108ffb799b04e94a28cc58f038f1" onerror="this.onerror=null;this.src='/blog/img/defaultCover.gif'" alt="iOS 如何在模拟器中安装APP"/></a><div class="content"><a class="title" href="/blog/2022/12/17/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/iOS%20%E5%A6%82%E4%BD%95%E5%9C%A8%E6%A8%A1%E6%8B%9F%E5%99%A8%E4%B8%AD%E5%AE%89%E8%A3%85APP/" title="iOS 如何在模拟器中安装APP">iOS 如何在模拟器中安装APP</a><time datetime="2022-12-17T07:48:56.594Z" title="更新于 2022-12-17 15:48:56">2022-12-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2021/08/05/Swift/swift%E5%B8%B8%E7%94%A8%E4%B8%89%E6%96%B9%E5%BA%93/" title="swift常用三方库"><img src= "" data-lazy-src="https://photos.infinum.com/store/b558bd40787ecad225d9c66322f14522" onerror="this.onerror=null;this.src='/blog/img/defaultCover.gif'" alt="swift常用三方库"/></a><div class="content"><a class="title" href="/blog/2021/08/05/Swift/swift%E5%B8%B8%E7%94%A8%E4%B8%89%E6%96%B9%E5%BA%93/" title="swift常用三方库">swift常用三方库</a><time datetime="2022-12-16T06:37:20.115Z" title="更新于 2022-12-16 14:37:20">2022-12-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/12/12/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/GCD%E8%BF%9B%E9%98%B6%E4%B9%8BDispatchWorkItem%E5%92%8CDispatchGroup/" title="GCD进阶之DispatchWorkItem和DispatchGroup"><img src= "" data-lazy-src="/blog/img/defaultCover.png" onerror="this.onerror=null;this.src='/blog/img/defaultCover.gif'" alt="GCD进阶之DispatchWorkItem和DispatchGroup"/></a><div class="content"><a class="title" href="/blog/2022/12/12/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6/GCD%E8%BF%9B%E9%98%B6%E4%B9%8BDispatchWorkItem%E5%92%8CDispatchGroup/" title="GCD进阶之DispatchWorkItem和DispatchGroup">GCD进阶之DispatchWorkItem和DispatchGroup</a><time datetime="2022-12-12T07:19:37.277Z" title="更新于 2022-12-12 15:19:37">2022-12-12</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimages2018.cnblogs.com%2Fblog%2F749924%2F201807%2F749924-20180730112644434-125751676.png&amp;refer=http%3A%2F%2Fimages2018.cnblogs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1635046008&amp;t=fd235aaa9fc1476b26197b2443c90864')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Albert Yuan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcom to my  blog</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><div class="search-dialog__title" id="algolia-search-title">Algolia</div><div id="algolia-input-panel"><div id="algolia-search-input"></div></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-stats"></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://fastly.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://fastly.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'REc8eytE1gJDl3d8FkudLNlL-gzGzoHsz',
      appKey: 'gGO3y8I9oVlc4Cd5N1fizNPW',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://fastly.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://fastly.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="255,0,0" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://fastly.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://fastly.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://fastly.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>